<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%!
	// 递归遍历标签
	public Map cate(List<Map> list, Map dest, List<Map> _dest) {
		List<Map> subs = (List<Map>)dest.get("sub");
		if (subs == null) {
			subs = new ArrayList<Map>();
			dest.put("sub", subs);
		}
		Integer _parent = Tools.getInt(dest.get("id")+"", -1001);
		Iterator<Map> iterator = list.iterator();
		while (iterator.hasNext()) {
			Map map = iterator.next();
			Integer parent = Tools.getInt(map.get("parent")+"", -1000);
			if (_parent.equals(parent)) {
				iterator.remove();
				_dest.add(map);
				List<Map> _list = new ArrayList<Map>();
				_list.addAll(list);
				subs.add(cate(_list, map, _dest));
			}
		}
		Collections.sort(subs, new Comparator<Map>(){
			public int compare(Map n1, Map n2) {
				return Tools.getInt(n1.get("show_order")+"", -1) - Tools.getInt(n2.get("show_order")+"", -1);
			}
		});
		dest.put("size", subs.size() + "");
		return dest;
	}
%>
<%
	Integer client = input.getInt("client", 1);		 // 1公众版 2院内版 3天下专家患者端 4天下专家医生端
	Integer level = input.getInt("level", -1);
	Integer parent = input.getInt("parent", -1);	 // 父类id,传0表示既没有大类也没有小类
	Integer uid = token.getUid();
	org.hibernate.StatelessSession statelessSession = DBTools.db("basedb").getSf().openStatelessSession();
	String sql= String.format("select * from (select ma.category as category, ma.id as id, ma.name as name, ma.icon as icon, ma.show_order as show_order, ma.parent_id as parent,m.content as latest_message , m.create_time as create_time from message_agent as ma,message as m where ma.hospital=%s and ma.status=1  and m.status=1 and ma.client=%s and m.uid=%s and m.category in (ma.category)  order by ma.show_order asc, m.id desc ) as t group by t.category", new Object[]{hospital, client, uid});
	List<Map> list = QBuilder.db("basedb").select("new map(id as id, name as name, icon as icon, show_order as show_order, parent_id as parent)").from("message_agent").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("client=", client)).order("show_order", "asc").query().getResultObj();
	List<Map> agents = new ArrayList<Map>();
	List<Map> noRead = new ArrayList<>();
	String noReadSql =String.format("select count(*) as msg_count, t.id from (select   ma.id as id , ma.category as category from message_agent as ma,message as m where ma.hospital=%s and ma.status=1 and m.status=1 and ma.client=%s and m.uid=%s and m.category in (ma.category) and m.confirm=0  order by ma.show_order asc, m.id desc ) as t group by t.category", new Object[]{hospital, client, uid});
	try {
		agents = statelessSession.createSQLQuery(sql).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		noRead = statelessSession.createSQLQuery(noReadSql).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		if (statelessSession != null)
			statelessSession.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	for (Map agent:list) {
		Integer id = (Integer)agent.get("id");
		agent.put("latest_message", "暂无消息");
		agent.put("msg_count", 0);
		for (Map msgAgent:agents) {
			Integer agentId = (Integer)msgAgent.get("id");
			if (id.equals(agentId)) {
				Long time = ((java.math.BigInteger)msgAgent.get("create_time")).longValue();
				agent.putAll(msgAgent);
				agent.put("create_time", DateUtil.formatDateByFormat(new Date(time), DateUtil.DATETIME_FORMAT));
			}
		}
		for (Map msgCount:noRead) {
			Integer agentId = (Integer)msgCount.get("id");
			if (id.equals(agentId))
				agent.putAll(msgCount);
		}
		agent.remove("category");
	}
	if (isEmpty(list)) {
		msg = new Message(30041, "暂无消息应用");
	} else {
		List<Map> dest = new ArrayList<Map>();
		Iterator<Map> iterator = list.iterator();
		if(level > 0) {  // 大于0需要多级分类
			List<Map> _dest = new ArrayList<Map>();
			while (iterator.hasNext()) {
				Map map = iterator.next();
				map.put("size", "0");
				map.put("sub", new ArrayList());
				Integer _parent = Tools.getInt(map.get("parent")+"", -1000000);
				if (_parent.equals(parent)) {
					iterator.remove();
					_dest.add(map);
					List<Map> _list = new ArrayList<Map>();
					_list.addAll(list);
					dest.add(cate(_list, map, _dest));
				}
			}
			list.removeAll(_dest);
			dest.addAll(list);
		} else {      // 小于等于0只需最后不同分类
			List<String> ids = new ArrayList<String>();
			List<String> parents = new ArrayList<String>();
			while (iterator.hasNext()) {
				Map map = iterator.next();
				ids.add(Tools.getDesc(map.get("id")));
				parents.add(Tools.getDesc(map.get("parent")));
			}
			ids.removeAll(parents);
			for (int i = 0; i < ids.size(); i++) {
				String id = ids.get(i);
				for (int j = 0; j < list.size(); j++) {
					Map map = list.get(j);
					String _id = Tools.getDesc(map.get("id"), "-------");
					if (_id.equals(id)) {
						dest.add(map);
						break;
					}
				}
			}
		}
		Collections.sort(dest, new Comparator<Map>(){
			public int compare(Map n1, Map n2) {
				// return Tools.getInt(n1.get("show_order")+"", -1) - Tools.getInt(n2.get("show_order")+"", -1);
				// 按照最新的消息应用置顶
				return Tools.getDesc(n2.get("create_time")+"", "0000-00-00 00:00:00").compareTo(Tools.getDesc(n1.get("create_time")+"", "0000-00-00 00:00:00"));
			}
		});
		if (dest.size() == 0)
			msg = new Message(30041, "暂无相关数据");
		msg.put("data", dest);
	}
	Tools.encryptPrint(out, msg);
%>
